import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/home/user_detail_entity.dart';
import 'package:ball_lion/entity/match/expert_views_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/server/api/expert_api.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/expert/items/plan_item.dart';
import 'package:ball_lion/widgets/card.dart';
import 'package:ball_lion/widgets/base/wqs_image.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class ExpertLeagueListPage extends StatefulWidget {
  const ExpertLeagueListPage({super.key});

  @override
  State<ExpertLeagueListPage> createState() => _ExpertLeagueListPageState();
}

class _ExpertLeagueListPageState extends State<ExpertLeagueListPage> {
  final ExpertLeagueItem? info = Get.arguments;
  List<PlanEntity>? data;
  int _page = 1;
  final EasyRefreshController controller =
      EasyRefreshController(controlFinishLoad: true);

  int get page => _page;

  set page(int value) {
    setState(() {
      _page = value;
    });
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  Future getData() async {
    page = 1;
    data = await ExpertApi.getExpertLeaguePlan(
        info?.expertId, info?.sportsId, info?.leagueId, 1);
    controller.resetFooter();
    setState(() {});
  }

  Future loadMore() async {
    page += 1;
    var result = await ExpertApi.getExpertLeaguePlan(
        info?.expertId, info?.sportsId, info?.leagueId, page);
    data?.addAll(result);
    controller.finishLoad((data?.length ?? 0) < page * 15
        ? IndicatorResult.noMore
        : IndicatorResult.success);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.greyF2F3F7,
      appBar: Styles.appBar(title: QsText(info?.expertName ?? "")),
      body: Column(
        children: [
          Row(
            children: [
              QsImage(
                imageUrl: info?.leagueLogo,
                width: 28,
                height: 28,
                fit: BoxFit.cover,
                placeholder: Styles.placeholderIcon(),
                errorWidget: Styles.placeholderIcon(),
              ),
              Container(width: 4),
              QsText(info?.leagueName ?? "", fontWeight: FontWeight.w500),
              const Spacer(),
              QsText(
                info?.content ?? "",
                fontWeight: FontWeight.w500,
              ),
            ],
          ).paddingSymmetric(horizontal: 8, vertical: 17),
          Expanded(
            child: EasyRefresh(
              controller: controller,
              onRefresh: getData,
              onLoad: loadMore,
              header: Styles.refreshHeader(),
              footer: Styles.refreshFooter(position: IndicatorPosition.locator),
              child: MediaQuery.removePadding(
                context: context,
                removeTop: true,
                child: ListView(
                    children: data
                            ?.map<Widget>((e) => Block(
                                cardStyle: const CardStyle(
                                    margin: EdgeInsets.fromLTRB(8, 0, 8, 8),
                                    padding:
                                        EdgeInsets.symmetric(horizontal: 12)),
                                child: PlanItem(
                                    entity: e, isExpertDetailView: true)))
                            .toList()
                            .append(const FooterLocator()) ??
                        []),
              ),
            ),
          )
        ],
      ),
    );
  }
}
